package com.camundademo.rest.service;

import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.repository.ProcessDefinitionQuery;
import org.camunda.bpm.engine.rest.dto.repository.ProcessDefinitionDto;
import org.camunda.bpm.engine.rest.dto.repository.ProcessDefinitionQueryDto;
import org.camunda.bpm.engine.rest.util.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CamundaProcessService {

    /**
     * 仓库服务,流程部署
     */
    @Autowired
    private RepositoryService repositoryService;

    /**
     * 流程引擎实例  可以获取各种服务
     */
    @Autowired
    private ProcessEngine processEngine;


    /**
     * 查询流程定义列表
     * 参考部署的代码实现的
     *
     * @param processDefinitionQueryDto
     * @param firstResult
     * @param maxResults
     * @return
     */
    public List<ProcessDefinitionDto> queryProcessDefinition(
            ProcessDefinitionQueryDto processDefinitionQueryDto,
            Integer firstResult,
            Integer maxResults
                                                            ) {

        ProcessDefinitionQuery query = (ProcessDefinitionQuery) processDefinitionQueryDto.toQuery(processEngine);
        List<ProcessDefinition> processDefinitionList = QueryUtil.list(query, firstResult, maxResults);
        List<ProcessDefinitionDto> reslist = new ArrayList();

        for (ProcessDefinition processDefinition : processDefinitionList) {
            ProcessDefinitionDto processDefinitionDto = ProcessDefinitionDto.fromProcessDefinition(processDefinition);
            reslist.add(processDefinitionDto);
        }

        return reslist;
    }
}
